#include "string.h"
#define BUFLEN 16
extern void clearScreen();
extern void poweroff();
extern void systime();
extern void drawPic();

// 启动界面
void startUp(){
  char *title = "My tiny OS V1.0";
  char *subTitle = "Designed by LWJ   ";
  char *copyRight = "copyright by GNU";
  char *hint = "System is ready. press Enter to start shell";
  clearScreen();
  printInPos(title, strlen(title), 5, 23);
  printInPos(subTitle, strlen(subTitle), 6, 29);
  printInPos(copyRight, strlen(copyRight), 8, 35);
  printInPos(hint, strlen(hint), 14, 11);
}

// 打印 shell 提示符
void promptStr(){
  char * pmt_str = "MyTinyOS #";
  print(pmt_str);
}

void showHelp(){
  char * helpStr = 
  "\nShell for MyTinyOS version 1.0 - on x86 PC \r\n"
  "This is a shell which is used for MyTinyOS."
  "These shell Commands are defined as follows."
  " Use \"help\" to see the list \r\n"
  "\r\n"
  "clear - clear the terminal \r\n"
  "time - get the current time \r\n"
  "drawpic - draw a animate picture \r\n"
  "shutdown - force shutdown the machine \r\n";
  print(helpStr);
}

void shell(){
  char cmdLine [BUFLEN - 1] = {0};
  char cmdDir [BUFLEN - 1] = {0};
  enum command {help,clear,time,drawpic,shutdown,enterpm};
  char* commands[] = {"help","clear","time","drawpic","shutdown","enterpm"};

  clearScreen();
  showHelp(); 
  while(1){
    promptStr();
    readToBuf(cmdLine,BUFLEN);
    getFirstWord(cmdLine,cmdDir);
    if(cmdDir[0] == 0x0d || strlen(cmdDir) == 0){
      continue;
    }else if(strcmp(cmdDir,commands[help]) == 0){
      showHelp();
    }else if(strcmp(cmdDir,commands[clear]) == 0){
      clearScreen();
    }else if(strcmp(cmdDir,commands[time]) == 0){
      systime();
    }else if(strcmp(cmdDir,commands[drawpic]) == 0){
      drawPic();
    }else if(strcmp(cmdDir,commands[shutdown]) == 0){
      poweroff();
    }else if(strcmp(cmdDir,commands[enterpm]) == 0){
      
    }else{
      print(cmdDir);
      print(" :command not found \r\n");
    }
  }
}